<template>
  <div class='container'>
    <el-card>
      <template>
      <div slot="header" v-if="subject.id && subject.name">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
        <el-row>
          <!-- 左 -->
          <el-col :span="16">
          <el-form :inline="true"  class="demo-form-inline" >
            <el-form-item label="标签名称">
              <el-input v-model="reqParamet.tagName" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="reqParamet.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="search()">搜索</el-button>
            </el-form-item>
          </el-form>
          </el-col>
          <!-- 右 -->
          <el-col :span="8">
            <el-button class="filter-item fr" type="success" icon="el-icon-edit"  @visible="dialogVisible = true" @click="openTags('add')">新增标签</el-button>
            <el-button class="filter-item fr" type="text" icon="el-icon-back" v-if="subject.id && subject.name" @click="$router.back()">返回</el-button>
          </el-col>
        </el-row>
        <!-- 提示 -->
        <el-alert
          v-if="total !== 0"
          :title="`数据共${total}条`"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
        <!-- 内容 -->
        <el-table
        :data="list"
        style="width: 100%">
          <el-table-column
            fixed
            type="index"
            :index="indexId"
            label="序号"
          >
          </el-table-column>
          <el-table-column
            fixed
            prop="subjectName"
            label="所属学科"
          >
          </el-table-column>
          <el-table-column
            fixed
            prop="tagName"
            label="标签名称"
          >
          </el-table-column>
          <el-table-column
            fixed
            prop="username"
            label="创建者"
          >
          </el-table-column>
          <el-table-column
            fixed
            prop="addDate"
            label="创建日期"
          >
          <!-- dayjs处理时间格式 -->
            <template #default="{row}">
              {{formatTime(row.addDate)}}
            </template>
          </el-table-column>
          <el-table-column
            fixed
            prop="state"
            label="状态"
          >
            <template #default="{ row }">
              {{row.state === 1? '启用':'禁用'}}
            </template>
          </el-table-column>

        <el-table-column label="操作">
          <template #default="{row}">
            <el-button type="text" @click="notUse(row)">{{row.state===1?'启用':'禁用'}}</el-button>
            <el-button :disabled="row.state==0" type="text" size="small" @click="openTags(row.id)">修改</el-button>
            <el-button :disabled="row.state==0" type="text" size="small" @click="delTags(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
        <div class="block">
          <el-pagination
            style="text-align:right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="reqParamet.page"
            :page-sizes="[5, 10, 20, 30, 40]"
            :page-size="reqParamet.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </template>

      <Dialog
      :dialogVisible="dialogVisible"
      @closeDialog="closeDialog"
      ref="add" />
    </el-card>
  </div>
</template>

<script>
import { list, changeState, remove, detail } from '@/api/hmmm/tags'
import Dialog from '../components/tags-add.vue'
import dayjs from 'dayjs'
export default {
  components: {
    Dialog
  },
  data () {
    return {
      list: [],
      reqParamet: {
        page: 1,
        pagesize: 10,
        subjectID: null,
        tagName: null,
        state: null
      },
      ruleForm: {
        user: '',
        region: ''
      },
      total: 0,
      dialogVisible: false
    }
  },
  created () {
    this.getlist()
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  methods: {
    formatTime (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    async getlist () {
      this.reqParamet.subjectID = this.subject.id || null
      const { data } = await list(this.reqParamet)
      // console.log(this.reqParamet)
      this.list = data.items
      this.total = data.counts
    },
    handleSizeChange (val) {
      this.reqParamet.page = 1
      // console.log(`每页 ${val} 条`)
      this.reqParamet.pagesize = val
      this.getlist()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.reqParamet.page = val
      this.getlist()
    },
    indexId (index) {
      index = index + (this.reqParamet.page - 1) * this.reqParamet.pagesize + 1
      return index
    },

    async openTags (id) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.add.openTags()
      })
      if (id !== 'add') {
        const { data } = await detail({ id })
        // console.log(data)
        this.$refs.add.form = data
      }
    },
    delTags (id) {
      this.$confirm('是否删除', '提示', {
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message.success('已删除')
        this.getlist()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    async notUse (data) {
      await changeState({
        id: data.id,
        state: data.state === 1 ? 0 : 1
      })
      data.state = data.state === 1 ? 0 : 1
      this.$message.success('操作成功')
    },
    search () {
      this.getlist()
      this.reqParamet.page = 1
    },
    clear () {
      this.reqParamet = {
        page: 1,
        pagesize: 10,
        subjectID: null,
        tagName: null,
        state: null
      }
    },
    closeDialog () {
      this.dialogVisible = false
      this.getlist()
      this.reqParamet.page = 1
      this.$refs.add.form = {
        subjectID: '',
        tagName: ''
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
